package com.activenetwork.smlite.testcase.events.eventListSection;

import java.util.Calendar;
import java.util.Date;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.XMLGregorianCalendar;


import org.openqa.selenium.By;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.schema.model.event.EventBasicType;
import com.activenetwork.schema.model.event.EventType;
import com.activenetwork.smlite.business.EventBusiness;
import com.activenetwork.smlite.page.event.EventsHomeView;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.verification.gson.FailureAssertion;
import com.activenetwork.util.verification.gson.FailurePoint;

public class EventListSection002 extends BaseCase {
static final Logger logger = LoggerFactory.getLogger(EventListSection002.class);

	@BeforeClass(alwaysRun = true)
	public void login(){
		loginBusiness.login();
	}
	
	@Test
	public void Verify_Events_that_login_user_has_access_to_are_listed_in_event_list_data_grid_correctly(){
		String eventName = createEventUnderAnotherAccount();

		EventsHomeView eventsHomeView = PageLoader.load(EventsHomeView.class);
		
		eventsHomeView.setSearchEvent(eventName);

		EventBusiness eventBusiness = new EventBusiness();

		eventBusiness.searchEvent(eventName);
		
		
		String msg = webDriver.findElement(By.cssSelector("div.nexus-text-center")).getText();
		if(!"No matched event! May you want to Add Event".equals(msg)){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					EventListSection002.class.getName(), 
					"Verify event list display data correctly", 
					"Msg not correct"));
		}

	}
	
	@Test
	public void creatEventViewInGrid() {
		
		String eventName = String.format("EventList002_event_same_account %d", new Date().getTime());

		EventsHomeView eventsHomeView = PageLoader.load(EventsHomeView.class);
		
		createEvent(eventName);
		
		eventsHomeView.events();

		eventsHomeView.setSearchEvent(eventName);

		EventBusiness eventBusiness = new EventBusiness();
		
		EventType event = new EventType();
		EventBasicType eventBasic = new EventBasicType();
		eventBasic.setTitle(eventName);
		event.setBasic(eventBasic);

		eventBusiness.searchEvent(eventName, event);
		
	}
	
	@AfterClass
	public void logoff(){
		loginBusiness.logout();
		FailureAssertion.pop(EventListSection002.class);
	}
	
	/**
	 * First let's create an event under Another account
	 */
	public String createEventUnderAnotherAccount() {
		loginBusiness.login("smlite@starcite.com", "st@rc!te");

		// generate a unique event name
		String eventName = String.format("EventList002_event_in_another_account %d", new Date().getTime());

		createEvent(eventName);

		loginBusiness.logout();
		
		return eventName;
	}
	
	private void createEvent(String eventName) {
		EventBusiness eventBusiness = new EventBusiness();

		EventType event = new EventType();

		EventBasicType eventBasic = new EventBasicType();

		event.setBasic(eventBasic);

		// event location
		EventBasicType.Location location = new EventBasicType.Location();
		location.setLocationName("smlite event location 001");
		location.setAddress("address line 1");
		location.setAddressContinue("address line 2");
		location.setCityRegion("Shanghai");
		location.setStateProvince("SH");
		location.setPostalCode("200032");
		location.setCounty("AG");
		location.setPrivate(true);
		eventBasic.setLocation(location);

		EventBasicType.Length.MultiDay dayInfo = new EventBasicType.Length.MultiDay();

		XMLGregorianCalendar startDate = convertDate(new Date());

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_WEEK, 3);

		XMLGregorianCalendar endDate = convertDate(calendar.getTime());

		dayInfo.setStartDate(startDate);
		dayInfo.setEndDate(endDate);

		EventBasicType.Length length = new EventBasicType.Length();
		length.setMultiDay(dayInfo);

		eventBasic.setLength(length);
		eventBasic.setTitle(eventName);

		eventBusiness.addEvent(event);
	}
	
	private XMLGregorianCalendar convertDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		javax.xml.datatype.DatatypeFactory dtf = null;

		try {
			dtf = javax.xml.datatype.DatatypeFactory.newInstance();
		}
		catch (DatatypeConfigurationException e) {
			throw new IllegalArgumentException(e);
		}

		XMLGregorianCalendar gCal = dtf.newXMLGregorianCalendar(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar
				.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND), calendar
				.get(Calendar.MILLISECOND), calendar.get(Calendar.ZONE_OFFSET) / (1000 * 60));

		return gCal;
	}
}
